/**
 * 
 */
package com.ideeli.webextractor.service;

import static com.ideeli.webextractor.utils.Constants.SUBMIT_URL;
import static com.ideeli.webextractor.utils.Constants.SEO_TOOL;
import static com.ideeli.webextractor.utils.Constants.SUBMIT_USER;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * @author sa2
 * 
 */
public class SubmitServiceImpl implements SubmitService
{

	private static final Logger logger = Logger
			.getLogger(SubmitServiceImpl.class);

	private CacheService cacheService;

	private FirefoxDriver driver;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ideeli.webextractor.service.SubmitService#submit()
	 */
	@Override
	public void submit(String pass) throws Exception
	{
		logger.info("Submitting..");
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

		logger.info("clicking base url..");
		driver.get(cacheService.getAppProperty(SUBMIT_URL));
		Thread.sleep(1000 * 3);
		
		logger.info("Sending user name..");
		driver.findElement(By.name("e")).clear();
		driver.findElement(By.name("e")).sendKeys(cacheService.getAppProperty(SUBMIT_USER));
		
		logger.info("sending pass..");
		driver.findElement(By.name("p")).clear();
		driver.findElement(By.name("p")).sendKeys(pass);
		
		
		driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();
		Thread.sleep(1000 * 5);
		
		logger.info("GO to Seo..");
		driver.findElement(By.linkText(cacheService.getAppProperty(SEO_TOOL))).click();
		Thread.sleep(1000 * 4);
		driver.findElement(By.linkText("1  Get In Google")).click();
		Thread.sleep(1000 * 4);
		driver.findElement(By.cssSelector("button.btn2")).click();
		Thread.sleep(1000 * 10);
		driver.findElement(By.linkText("2  Google Blacklist Check")).click();
		Thread.sleep(1000 * 5);
		driver.findElement(By.cssSelector("button.btn2")).click();
		Thread.sleep(1000 * 8);
		driver.findElement(By.linkText("3  Link Building")).click();
		Thread.sleep(1000 * 8);
		driver.findElement(By.id("formstart")).click();
		Thread.sleep(1000 * 10);
		driver.findElement(By.xpath("//button[@type='button']")).click();
		Thread.sleep(1000 * 10);
		driver.findElement(By.linkText("4  See Your New Listings")).click();
		Thread.sleep(1000 * 10);
		driver.findElement(By.cssSelector("button.btn2")).click();
		Thread.sleep(1000 * 10);
		driver.findElement(By.linkText("sign out")).click();
		Thread.sleep(1000 * 5);
		driver.quit();
		logger.info("Finished submitting map!!");

	}

	public CacheService getCacheService()
	{
		return cacheService;
	}

	public void setCacheService(CacheService cacheService)
	{
		this.cacheService = cacheService;
	}

}
